﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Reflection;
using Telerik.Web.UI;
using System.Data.SqlClient;


public partial class Admin_Report : System.Web.UI.Page
{
    decimal Total = 0;
    int quantityTotal = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindVendor();
        }
    }
    private void BindVendor()
    {
        var deal = new DealesDataContext();
        var Vendor = deal.Vendor_Return();
        dropVendor.DataTextField = "VendorName";
        dropVendor.DataValueField = "VendorID";
        dropVendor.DataSource = Vendor;
        dropVendor.DataBind();
        dropVendor.Items.Insert(0, new ListItem("-Select-", "0"));

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //RadGrid1.MasterTableView.Items[0].Expanded = true;
            //RadGrid1.MasterTableView.Items[0].ChildItem.FindControl("InnerContainer").Visible = true;
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {

        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        {
            Label Price = e.Item.FindControl("lblPrice") as Label;
            Label Date = e.Item.FindControl("lblDate") as Label;
            Price.Text = "<b>Regular Price: $</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "RegularPrice")) + "<br/>" + "<b> Sell Price: $</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "SellPrice")) + "<br/>" + "<b>Discount: $</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Discount"));
            Date.Text = "<b>Sell Start Date : </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "SellStartDate")).ToShortDateString()) + "<br/>" + "<b>Sell End Date: </b>" + Convert.ToString(Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "SellEndDate")).ToShortDateString());


        }
        if (e.Item is GridNestedViewItem)
        {
            var deal = new DealesDataContext();
            var Vendor = deal.Select_VendorByID(Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "VendorID")));
            DetailsView DetailsView1 = e.Item.FindControl("DetailsView1") as DetailsView;
            DetailsView1.DataSource = Vendor;
            DetailsView1.DataBind();

            var DealDetail = deal.Select_OrderByDealIDForReport(Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "DealID")));
            RadGrid OrdersGrid = e.Item.FindControl("OrdersGrid") as RadGrid;
            OrdersGrid.DataSource = DealDetail;
            OrdersGrid.DataBind();

            Total = 0;
            quantityTotal = 0;
            GridView GridView1 = e.Item.FindControl("GridView1") as GridView;
            Button Button1 = e.Item.FindControl("Button1") as Button;
            Button Button2 = e.Item.FindControl("Button2") as Button;
            GridView1.DataSource = DealDetail;
            GridView1.DataBind();
            if (GridView1.Rows.Count > 0)
            {
               
                Button1.Visible = true;
               
            }
            else
            {
                Button1.Visible = false;
               
               
            }

        }

    }
    protected string TrimDescription(string description)
    {
        string des = System.Text.RegularExpressions.Regex.Replace(description, "<[^>]*>", "");
        if (!string.IsNullOrEmpty(des) && des.Length > 50)
        {
            return string.Concat(des.Substring(0, 20), "...");
        }
        return des;
    }
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            ((GridDataItem)e.Item).ChildItem.FindControl("InnerContainer").Visible =
                !e.Item.Expanded;
        }
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        //var editableItem = ((GridNestedViewItem)e.Item);
        //var VendorID = (long)editableItem.GetDataKeyValue("VendorID"); 
        //int VendorID = ;
        if (e.Item is GridNestedViewItem)
        {

            e.Item.FindControl("InnerContainer").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;


        }
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        RadGrid1.Visible = true;
        var deal1 = new DealesDataContext();
        var Vendor1 = deal1.Select_DealByVendorIDForReport(Convert.ToInt32(dropVendor.SelectedValue), Convert.ToDateTime(RadDatePicker2.SelectedDate), Convert.ToDateTime(RadDatePicker1.SelectedDate));
        RadGrid1.DataSource = Vendor1;
        RadGrid1.DataBind();
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {


        GridNestedViewItem editItem = (GridNestedViewItem)((Button)sender).NamingContainer;
        GridView GridView1 = editItem.FindControl("GridView1") as GridView;
        string DealTitle = GridView1.DataKeys[0]["DealTitle"].ToString();
        string DealID = GridView1.DataKeys[0]["DealID"].ToString();
        string ExcelID = dropVendor.SelectedValue + "_" + DealID + "_" + System.DateTime.Now.ToShortDateString()+".xls";
        GridViewExportUtil.Export(ExcelID, GridView1, DealTitle, dropVendor.SelectedItem.Text, Convert.ToDateTime(RadDatePicker1.SelectedDate).ToShortDateString(), Convert.ToDateTime(RadDatePicker2.SelectedDate).ToShortDateString());
    }

   

     

    protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TotalPurchaseQty"));
            Total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "NetAmount"));
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[8].Text = "Total Amount: " + Total.ToString("c");
            e.Row.Cells[4].Text = "Total Quantity: " + quantityTotal.ToString();
            
        }
    }
}

